"use strict";

var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };

function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }

var app = angular.module("myApp", ['ui.bootstrap']);

app.controller("reportController", ['$scope', '$compile', '$sce', function ($scope, $compile, $sce) {
    $scope.toJSON = function (val) {
        return toJSON(val);
    };
    var request = getRequest();
    $scope.faultId = request["faultId"];
    // $scope.featureIds = JSON.parse(request["featureIds"]);
    console.info(request["matchedNodes"]);
    $scope.matchedNodes = JSON.parse(request["matchedNodes"]);
    $scope.timestamp = new Date(parseInt(request["timestamp"]));

    $scope.sequence = '001232016072010010121001';
    $scope.reasons = [];

    var queryUrl = "start n=node(" + $scope.faultId + ") match (n)-[*]->(p) return n,p";
    $scope.faultUrl = $sce.trustAsResourceUrl("../browser/NeoEmbed.html?query=" + encodeURIComponent(queryUrl).replace(/=/g, '%3d'));

    Graph.loadGraph($scope.faultId, function (g) {
        $scope.graph = g;
        var nodesList = [].concat(_toConsumableArray(g.nodes.values()));

        var _loop = function _loop(nd) {
            if ($scope.matchedNodes.some(function (nid) {
                return nid == nd.id;
            })) {
                nd.isMatched = true;
            }
        };

        var _iteratorNormalCompletion = true;
        var _didIteratorError = false;
        var _iteratorError = undefined;

        try {
            for (var _iterator = nodesList[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
                var nd = _step.value;

                _loop(nd);
            }
        } catch (err) {
            _didIteratorError = true;
            _iteratorError = err;
        } finally {
            try {
                if (!_iteratorNormalCompletion && _iterator.return) {
                    _iterator.return();
                }
            } finally {
                if (_didIteratorError) {
                    throw _iteratorError;
                }
            }
        }

        $scope.fault = nodesList.filter(function (node) {
            return node.$type == "Fault";
        })[0];
        $scope.features = nodesList.filter(function (node) {
            return node.$type == 'Feature';
        }).sort(function (a, b) {
            return a > b;
        });
        $scope.features.forEach(function (f, index) {
            ComponentNode.loadComponentsFrom(f.id, function (components) {
                $scope.features[index].components = components;
                $scope.$apply();
            });
        });
        var matchedOutIds = $scope.features.filter(function (f) {
            return f.isMatched;
        }).map(function (f) {
            return f.outId;
        }).filter(function (id) {
            return id != 0;
        });
        console.info(matchedOutIds);
        $scope.reasons = nodesList.filter(function (node) {
            return node.$type == 'Reason';
        }).sort(function (a, b) {
            return a > b;
        });
        $scope.reasons.filter(function (r) {
            return matchedOutIds.some(function (id) {
                return id == r.outId;
            });
        }).forEach(function (r) {
            return r.isMatched = true;
        });
        $scope.$apply();
    });

    $scope.getNow = function () {
        return $scope.timestamp;
    };
}]);

var toJSON = function toJSON(obj) {
    var cache = [];
    return JSON.stringify(obj, function (key, value) {
        if ((typeof value === "undefined" ? "undefined" : _typeof(value)) === 'object' && value !== null) {
            if (cache.indexOf(value) !== -1) {
                // Circular reference found, discard key
                return;
            }
            // Store value in our collection
            cache.push(value);
        }
        return value;
    });
};

function getRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = {};
    if (url.indexOf("?") !== -1) {
        var str = url.substr(1);
        var strs = str.split("&");
        for (var i = 0; i < strs.length; i++) {
            theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}
